﻿namespace RefactorExpressions
{
    using System;

    public class Figure
    {
        private double width;
        private double height;

        public Figure(double width, double height)
        {
            this.Width = width;
            this.Height = height;
        }

        public double Width
        {
            get
            {
                return this.width;
            }

            private set
            {
                if (value <= 0)
                {
                    throw new ArgumentOutOfRangeException("Incorrect width");
                }

                this.width = value;
            }
        }

        public double Height
        {
            get
            {
                return this.height;
            }

            private set
            {
                if (value <= 0)
                {
                    throw new ArgumentOutOfRangeException("Incorrect height");
                }

                this.height = value;
            }
        }

        internal void Rotate(double angle, bool changeCurrentInstance)
        {
            if (changeCurrentInstance == true)
            {
                this.RotateCurrentInstance(angle);
            }
            else
            {
                this.RotateNewInstance(angle);
            }
        }

        private void RotateCurrentInstance(double angle)
        {
            // separate variables because if we initialize the new values to the properties the changes of width will effect the calculations for height
            double width = ((Math.Abs(Math.Cos(angle)) * this.Width) + (Math.Abs(Math.Sin(angle)) * this.Height));
            double height = ((Math.Abs(Math.Sin(angle)) * this.Width) + (Math.Abs(Math.Cos(angle)) * this.Height));

            this.Width = width;
            this.Height = height;
        }

        private Figure RotateNewInstance(double angle)
        {
            double width = ((Math.Abs(Math.Cos(angle)) * this.Width) + (Math.Abs(Math.Sin(angle)) * this.Height));
            double height = ((Math.Abs(Math.Sin(angle)) * this.Width) + (Math.Abs(Math.Cos(angle)) * this.Height));

            return new Figure(width, height);
        }
    }
}